package com.opgea.tutor.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.opgea.tutor.dao.AbstractDAO;
import com.opgea.tutor.dao.TutorDAO;
import com.opgea.tutor.domain.entities.Tutor;

@Repository
public class TutorDAOImpl extends AbstractDAO<Tutor> implements TutorDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public Tutor findTutorByLoginId(Long id) {
	Query query = sessionFactory.getCurrentSession().createQuery(
		"from Tutor t WHERE t.login.id = :id");
	query.setParameter("id", id);
	if (query.list() != null && query.list().size() > 0) {
	    return (Tutor) query.list().get(0);
	} else {
	    return null;
	}
    }

    @Override
    public Tutor find(Long id) {
	@SuppressWarnings("unchecked")
	List<Tutor> objects = sessionFactory.getCurrentSession()
		.createCriteria(Tutor.class).add(Restrictions.eq("id", id))
		.list();
	System.out.println(objects.get(0));
	return objects.get(0);
    }

    @SuppressWarnings("unchecked")
    public List<Tutor> findAll() {
	Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
		Tutor.class);
	List<Tutor> objects = criteria.list();
	return objects;
    }

}
